package com.jfinal.weixin.sdk.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.weixin.sdk.msg.out.News;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by 446705260@qq.com on 16/5/4.
 * chengzhichao
 */
public class NewsApi {


    public static List<News> getNewsByJuhe(String cityname){
        //String httpUrl = "http://apis.baidu.com/songshuxiansheng/real_time/search_news";
        String httpUrl = "http://op.juhe.cn/onebox/news/query";
        //String httpArg = "keyword="+cityname+"&count=10";
        String httpArg = "q="+cityname+"&dtype=&key=f64f3d33d7cbe5a0b51f0973af147708";
        String jsonResult = request(httpUrl, httpArg);
        System.out.print(jsonResult);
        JSONObject jobj =  JSONObject.parseObject(jsonResult);
        jsonResult = jobj.get("result").toString();
//        jobj =  JSONObject.parseObject(jsonResult);
//        jsonResult = jobj.get("data").toString();
        JSONArray data = JSONArray.parseArray(jsonResult);
        List list = new ArrayList();
        List index = new ArrayList();
        while (index.size()<3){
            int x=(int)(new Random().nextInt(data.size()));
            if (!index.contains(x)){
                jobj =  (JSONObject) data.get(x);
                News news = new News();
                String img = jobj.getString("img");
                if(img.equals("")||img==null){
                    img="http://www.0713jc.com/img/icon.jpeg";
                }
                news.setPicUrl(img);
                news.setTitle(jobj.getString("title"));
                news.setUrl(jobj.getString("url"));
                list.add(news);
                index.add(x);
            }
        }
        return list;
    }

    public static List<News> getNewsByBaidu(String cityname){
        String httpUrl = "http://apis.baidu.com/songshuxiansheng/real_time/search_news";

        String httpArg = "keyword="+cityname+"&count=10";

        String jsonResult = request(httpUrl, httpArg);
        System.out.print("》》》》》》》》"+jsonResult+">>>>>>"+cityname);
        JSONObject jobj =  JSONObject.parseObject(jsonResult);
        jsonResult = jobj.get("retData").toString();
        jobj =  JSONObject.parseObject(jsonResult);
        jsonResult = jobj.get("data").toString();
        JSONArray data = JSONArray.parseArray(jsonResult);
        List list = new ArrayList();
        List index = new ArrayList();
        while (index.size()<5){
            int x=(int)(new Random().nextInt(data.size()));
            if (!index.contains(x)){
                jobj =  (JSONObject) data.get(x);
                News news = new News();
                String img = jobj.getString("img_url");
                if(img.equals("")||img==null){
                    img="http://www.0713jc.com/img/icon.jpeg";
                }
                news.setPicUrl(img);
                news.setTitle(jobj.getString("title"));
                news.setUrl(jobj.getString("url"));
                list.add(news);
                index.add(x);
            }
        }
        return list;
    }

    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            // 填入apikey到HTTP header
            connection.setRequestProperty("apikey",  "f3d61b0fb8a6a4ad199a37af2fe6230a");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
